<template>
  <!-- 选择变更清单 -->
  <el-dialog title="选择变更清单" :visible.sync="visible" :close-on-click-modal="false" width="1000px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="清单编号" prop="boqCode">
        <el-input
          v-model="queryParams.boqCode"
          placeholder="请输入清单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="清单名称" prop="boqName">
        <el-input
          v-model="queryParams.boqName"
          placeholder="请输入清单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="meterageList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="清单编码" align="left" prop="boqCode" :show-overflow-tooltip="true" />
        <el-table-column label="清单名称" align="left" prop="boqName" :show-overflow-tooltip="true" />
        <el-table-column label="单位" align="center" prop="unit" />
        <el-table-column label="起始桩号" align="center" prop="beginStation" />
        <el-table-column label="截止桩号" align="center" prop="endStation" />
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="数量" align="center" prop="amount" />
        <el-table-column label="金额" align="center" prop="total" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectMeterage">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { unallocatedMeterageList, save } from "@/api/engineering/changeDetail";
export default {
  props: {
    // 主表ID
    changeId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      meterageBoqId: [],
      // 总条数
      total: 0,
      // 未选择计量清单
      meterageList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        changeId: undefined,
        boqCode: undefined,
        boqName: undefined
      }
    };
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams.changeId = this.changeId;
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.meterageBoqId = selection.map(item => item.meterageBoqId);
    },
    // 查询表数据
    getList() {
      unallocatedMeterageList(this.queryParams).then(res => {
        this.meterageList = res.rows;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择计量清单操作 */
    handleSelectMeterage() {
      const changeId = this.queryParams.changeId;
      const ids = this.meterageBoqId.join(",");
      save({ changeId: changeId, ids: ids }).then(res => {
        this.$modal.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      })
    }
  }
};
</script>
